//
//  WebViewController.m
//  XDEducation
//
//  Created by Ethan on 2017/5/13.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "WebViewController.h"
#define kScreenWith [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#import "UserManager.h"
#import "Account.h"
#import "AccountTool.h"
#import "UIBarButtonItem+XDItem.h"
@interface WebViewController ()<UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) UIBarButtonItem *rightButtonItem;
@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.nameTitle;
    self.rightButtonItem = [UIBarButtonItem  itemWithImage:@"nav_icon_esc_default" highImage:@"nav_icon_esc_default" target:self action:@selector(rightButtonItemAction)];
    self.navigationItem.rightBarButtonItem = self.rightButtonItem;
    
    
    [self loadWeb:self.str];

}

- (void)rightButtonItemAction{
    
    
    
}

- (void)loadWeb:(NSString *)url {
    
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWith, kScreenHeight - 64)];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    [self.view addSubview:self.webView];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    NSURLRequest *reque = [self makeTheCook:request];
    
    [self.webView loadRequest:reque];
    
}


-  (NSURLRequest *)makeTheCook:(NSMutableURLRequest *)requst {
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *str = [defaults objectForKey:@"authCode"];
    
    NSMutableDictionary *properties = [NSMutableDictionary dictionary] ;
    [properties setValue:str forKey:NSHTTPCookieValue];
    [properties setValue:@"uid" forKey:NSHTTPCookieName];
    [properties setValue:@"client.xing6688.com" forKey:NSHTTPCookieDomain];
    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
    [properties setValue:@"/" forKey:NSHTTPCookiePath]; // /StarShine  /
    NSHTTPCookie *cook = [NSHTTPCookie cookieWithProperties:properties];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cook];
    
    return requst;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    [MBProgressHUD showHUD];
    
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [MBProgressHUD hideHUD];
}





















-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    NSLog(@"%@",error);
    //[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    
    
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    if (self.Back)
    {
        self.Back ();
    }
}
@end
